package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> pageQuery(Integer page, Integer size) {
        log.info("pageQuery ..");
        Page<Item> p = itemService.page(new Page<>(page, size), new LambdaQueryWrapper<Item>().orderByDesc(Item::getUpdateTime));
        return new PageDTO<>(p.getTotal(), p.getRecords());
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id) {
        log.info("getById ..");
        return itemService.getById(id);
    }

    /**
     * 新增
     * @param item
     * @return
     */
    @PostMapping
    public boolean save(@RequestBody Item item) {
        log.info("save ..");
        Item item1 = new Item();
        BeanUtils.copyProperties(item, item1);
        return itemService.save(item1);
    }

    /**
     * 修改商品状态
     * 上架 下架
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public boolean startOrStop(@PathVariable Long id, @PathVariable Integer status) {
        log.info("startOrStop ..");
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        return itemService.updateById(item);

        // TODO 2将id和status封装成map; 发送给队列;

//        Map<String, Long> map = new HashMap<>();
//        map.put("id", id);
//        map.put("status", status.longValue());
//        rabbitTemplate.convertAndSend("search.direct", "startStop", map, RabbitMQConfig.getCorrelationData());


    }

    /**
     * 更新商品
     * @param item
     * @return
     */
    @PutMapping
    public boolean updateById(@RequestBody Item item) {
        log.info("updateById ..");
        Item item1 = new Item();
        BeanUtils.copyProperties(item, item1);
        return itemService.updateById(item1);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        log.info("delete ..");

        return itemService.removeById(id);
    }

    @Transactional
    @PutMapping("/stock/{itemId}/{num}")
    public boolean deduct(@PathVariable Long itemId, @PathVariable Integer num) {

        // TODO 完成扣减库存;

        return false;
    }
}
